﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 一.二
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] ar = { 1, 5, 8, 6 };
            array a = new array(ar);
            a.output();
            a.insert();//测试插入
            a.output();
            Console.WriteLine("***测试多次插入操作***");
            a.insert();
            a.output();
            a.search();

        }
    }
    class array
    {
        public array(int[] array)//构造函数
        {

            Array = array;

        }
        public void output()//输出打印
        {
            Console.WriteLine("打印数组：");
            foreach (int i in Array)
                Console.WriteLine($"{i}\t");

        }
        public void insert()//插入功能
        {
            int locate;//插入位置
            int value;//插入值
            bool k = true;
            while (k)
            {
                Console.WriteLine("输入插入的数字:");
                string s = Console.ReadLine();
                value = int.Parse(s);
                Console.WriteLine("输入要插入的位置：");
                s = Console.ReadLine();
                locate = int.Parse(s);
                if (locate > Array.Length)
                    Console.WriteLine("输入错误！请重新输入：");
                else
                {
                    int[] temp;//用来复制数组Array，并完成插入
                    temp = new int[Array.Length + 1];
                    int i = 0, j = 0;
                    int index = 1;
                    while (index < locate)
                    {
                        temp[i++] = Array[j++];
                        index++;
                    }
                    temp[i++] = value;
                    while (j != Array.Length)
                    {
                        temp[i++] = Array[j++];
                    }
                    Array = temp;//将插入完成的数组temp重新复制给Array
                    Console.WriteLine("操作成功！");
                    k = false;
                }
            }

        }
        public void search()//查找
        {
            int value;
            bool k = true;
            while (k)
            {
                int i;
                int index = 0;
                Console.WriteLine("输入要查找的的值:");
                string s = Console.ReadLine();
                value = int.Parse(s);
                for (i = 0; i < Array.Length; i++)
                {
                    if (Array[i] == value)
                    {
                        index = i;
                        k = false;
                        break;
                    }

                }
                if (k == true)
                    Console.WriteLine($"没找到值{value}!等待重新输入查找值");
                else
                    Console.WriteLine($"值{value}的下标为：{index}");
            }


        }
        int[] Array;


    }

}